package com.qianbao.service.system.impl;

import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.excepion.ExceptionEnum;
import com.qianbao.dao.system.AppConfigDao;
import com.qianbao.dao.system.AppUpdateDao;
import com.qianbao.domain.system.AppConfigEntity;
import com.qianbao.domain.system.AppUpdateEntity;
import com.qianbao.service.system.SVSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class SVSystemServiceImpl implements SVSystemService {

    @Autowired
    private AppConfigDao configDao;

    @Autowired
    private AppUpdateDao updateDao;


    @Override
    public String findSystemTime() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String currentDate = sdf.format(new Date());

        return currentDate;
    }

    @Override
    public AppConfigEntity findSystemAppConfig(String version) throws CommonException{

        if (version == null || version.length() <= 0){
            throw new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),
                    ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        AppConfigEntity appConfig = configDao.findSystemAppConfig(version);

        return appConfig;
    }

    @Override
    public AppUpdateEntity findSystemUpdate(String version) throws CommonException {

        if (version == null || version.length() <= 0){
            throw new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),
                    ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        AppUpdateEntity updateEntity = updateDao.findSystemUpdate(version);

        return updateEntity;
    }
}
